import javax.xml.soap.Node;

/**
 * Created with IntelliJ IDEA.
 * User:xieChao
 * Date:2022/10/24 9:10
 */


/**
 * 二叉搜索数底层实现
 */

class TreeNode{
    public  int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int val){
        this.val = val;
    }
}


public class BinarySearchTree {
    public TreeNode root = null;
    public TreeNode search(int key){
        TreeNode cur = root;
        while(cur != null){
            if(cur.val > key){
                cur = cur.left;
            }else if(cur.val == key){
                return cur;
            }else {
                cur = cur.right;
            }
        }
        return null;
    }

    public boolean insert(int val){
        if(root == null){
            root = new TreeNode(val);
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null){
            if(cur.val > val){
                parent = cur;//保证父亲节点在cur前一个
                cur = cur.left;
            } else if (cur.val < val) {
                parent = cur;
                cur = cur.right;
            }else{
                return false;
            }
        }
        TreeNode node = new TreeNode(val);
        if(parent.val > val){
             parent.left = node;
        }else{
            parent.right = node;
        }
        return true;
    }

    public void remove(int key){
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null){
            if(cur.val == key){
                removeNode(cur,parent);
                break;
            }else if(cur.val > key){
                parent = cur;
                cur = cur.left;
            }else{
                parent = cur;
                cur = cur.right;
            }
        }
    }

    public void removeNode(TreeNode cur,TreeNode parent){
        if(cur.left == null){
            if(cur == root){
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            }else{
                parent.right = cur.right;
            }
        }else if(cur.right == null){
            if(cur == root){
                root = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            }else{
                parent.right = cur.left;
            }
        }else{
            //cur.left != null && cur.right != null;
             TreeNode tap =cur;
             TreeNode ta = cur.right;
             while (ta.left != null){
                tap = ta;
                ta = ta.left;
             }
             cur.val = ta.val;
             if(ta == tap.left) {
                 tap.left = ta.right;
             }else{
                 tap.right = ta.right;
             }
        }
    }

    public void inOrder(TreeNode root){
        if(root == null) return;
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    public static void main(String[] args) {
        int[] arr = {10,8,19,3,9,4,7};
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        for (int i = 0; i < arr.length; i++) {
            binarySearchTree.insert(arr[i]);
        }
        binarySearchTree.inOrder(binarySearchTree.root);
    }

}
